package com.otitan.la.forest.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otitan.la.forest.system.dto.PermissionDTO;
import com.otitan.la.forest.system.entity.PermissionEntity;
import com.otitan.la.forest.system.service.PermissionService;
import com.otitan.la.forest.system.vo.PermissionPageVO;
import com.otitan.la.forest.system.vo.PermissionVO;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 动作元素表
 *
 * @author jiacs
 * @email xxx
 * @date 2020-09-17 14:26:54
 */
@Slf4j
@Api(tags = "动作元素表API")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "新增")
    @PostMapping
    public DataResult add(@Valid @RequestBody PermissionEntity permission, BindingResult bindingResult) {
        return permissionService.add(permission, bindingResult);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {
        return permissionService.batchDelete(ids);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return permissionService.batchDelete(ids);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public DataResult editor(@Valid @RequestBody PermissionEntity permission, BindingResult bindingResult) {
        return permissionService.editor(permission, bindingResult);
    }

    /**
    * 按钮分业务列表查询
    * @author Sunke
    * @param [  page, size, permissionDTO]
    * @return com.otitan.webapp.framework.basepro.model.DataResult
    * @exception
    * @date 2020/9/21 0021 10:06
    */
    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/findPage/{page}/{size}")
    public DataResult<Page<PermissionPageVO>> findPageData(@PathVariable("page") Long page,
                                                           @PathVariable("size") Long size,
                                                           @RequestBody PermissionDTO permissionDTO) {
        return permissionService.findPageData(page, size,permissionDTO);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/findOne/{id}")
    public DataResult<PermissionVO> findOne(@PathVariable("id") String id) {
        return permissionService.findOne(id);
    }

}
